﻿///对象指针
///用一个全局id代表一个对象
var pointer = function () {
    this._id = null;            //指针id
    this._states = {};          //指针状态，可包含多种
    this._o = null;             //指向对象
    this._upid = null;          //创建者id
    this._downintent = null;    //创建意图
    this._listens = [];         //回发监听组 item结构:{intent:..,hander:..}
    this._contains = [];        //包含的指针，当自身释放时，应先释放包含的指针
    this.id = function () {
        if (arguments.length == 1) {
            this._id = arguments[0];
        }
        return this._id;
    }
    this.o = function () {
        if (arguments.length == 1) {
            this._o = arguments[0];
        }
        return this._o;
    };
    this.downintent = function () {
        if (arguments.length == 1) {
            this._downintent = arguments[0];
        }
        return this._downintent;
    };
    this.up = function () {
        if (arguments.length == 1) {
            this._upid = arguments[0];
        }
        return mPointerManager.get(this._upid);
    };
    this.state = function (key, value) {
        if (arguments.length == 2) {
            this._states[key] = value;
        }
        if (arguments.length == 1) {
            return this._states[key];
        }
    };
    this.notify = function (intent, obj) {
        for (var i = 0; i < this._listens.length; i++) {
            if (this._listens[i].intent == intent) {
                this._listens[i].hander(this, obj);
            }
        }
    };
    this.listen = function (intent, hander) {
        this._listens.push({ intent: intent, hander: hander });
    };
    this.removelisten = function (intent) {
        for (var i = 0; i < this._listens.length; i++) {
            if (this._listens[i].intent == intent) {
                this._listens = [].concat(this._listens.slice(0, i), this._listens.slice(i + 1));
                break;
            }
        }
    };
    this.release = function () {
        if (this._contains != null) {
            for (var i = 0; i < this._contains.length; i++) {
                var p = mPointerManager.get(this._contains[i]);
                if (p) {
                    p.release();
                }
            }
        };
        mPointerManager.remove(this._id);
    };
    this.contain = function (id) {
        var isin = false;
        for (var i = 0; i < this._contains.length; i++) {
            if (this._contains[i] == isin) {
                isin = true;
                break;
            }
        }
        if (!isin) {
            this._contains.push(id);
        }
    };

    if (arguments.length == 1) {
        this.o(arguments[0]);
    }
    if (arguments.length == 2) {
        this.id(arguments[0]);
        this.o(arguments[1]);
    }
    if (arguments.length == 3) {
        this.id(arguments[0]);
        this.o(arguments[1]);
        this.downintent(arguments[2]);
    }
    if (this.id() == null) {
        this.id(Utils.getGuid());
    }


    this.listen(intent.action.call, function (_this, _arg) {
        _this.o()[_arg.call](_arg.data);
    });
};
